/**
 * DATA PROJECT
 */
var currentProject = {
	id:0,
	name:"",
	urlIframe:null
}
var curFile = "";
var caseProject = {
	renderItemProject: function(data){
		if(typeof data != "undefined"){
			var html = '<div class="css-treeview css-treeview-project div-item'+data.id+'" uuid="'+data.id+'" >'
				+'<ul>'
					+'<li class="iteam li-project-name" name="'+data.name+'" uuid="'+data.id+'">'
						+'<input type="checkbox", id="something'+data.id+'" />'
						+'<label for="something" class="nameproject">'+data.name+'</label>'
						+'<i class="fa fa-trash-o trash-i class_remove_project" uuid="'+data.id+'"  ></i>'
						+'<ul class="dirproject">'+data.dir+'</ul>'
					+'</li>'
				+'</ul>'
			+'</div>';

			$("#stencils-holder").append(html);
		}
	},
	openMenu: function(){
		$(".menu-upload-project").show();
	},	
	closeMenu: function(){
		$(".menu-upload-project").hide();
	},
	openBoardUpload: function(){
		$(".cms-upload").show();
		$(".cms-board").hide();
		$(".cms-public").hide();
	},
	openBoardPublic: function(){
		$(".cms-upload").hide();
		$(".cms-board").hide();
		$(".cms-public").show();
	},	
	openBoardLayouts: function(){
		jLayouts('We\'re limit '+ptLayouts.limit+' layouts, you can <a href="#">upgrate account</a> add layout more. Click on fields to edit layout name', '', 'Layouts', function(r) {
			if( r ){
				alert(r)
			}
		});
	},
	closeBoardUpload: function(){
		$(".cms-upload").hide();
		$(".cms-public").hide();
		$(".cms-board").show();
	},
	fileNotOpen:function(ext){
		$(".cms-board").html("<div class='fnopen' >The file extension ("+ext+") can't open!</div>");
	},
	viewFileIframe:function(name){
		
		
		
		caseProject.toggleLoading(true);
		$.get('/api/iframe/view?pid='+currentProject.id+"&name="+name,function(data){
			caseProject.toggleLoading(false);
			if(data.error){
				showStickyErrorToast(data.message);
			}else{
				var data = data.data;
				currentProject.urlIframe = "/projects/"+currentUser.user.id+"/"+data.name+"/"+name+"";
				$(".cms-board").html("<iframe id='project_frame' src='"+currentProject.urlIframe+"' width='100%' height='100%'></iframe>");
				try{
					ptCms.init();
				}catch(e){
					console.log("Error Iframe: "+e.message());
				}
			}
		}).error(function(data, status, headers, config) {
			if(status=="error") showStickyErrorToast(data.responseText);
			caseProject.toggleLoading(false);
		});
	},
	openBoard: function(){

	
	},
	setColorItem:function(_object){
		$.each($("#stencils-holder ul li.iteam"),function(x,y){
			$(y).find('.nameproject').css({color:'#282828'});
		});
		_object.find('.nameproject').css({color:'#FA5858'});
	},
	toggleLoading:function(n){
		if(n) $(".loading").css({opacity:1});
		else $(".loading").css({opacity:0});
	},
	search:function(q){
		caseProject.toggleLoading(true);
		$.get("/api/projects/search?q="+q,{})
		.success(function(data, status, headers, config) {
			
			if(data.error){
				
				
			}else{
				console.log(data);
				caseProject.closeBoardUpload();
				caseProject.closeMenu();
				$("#stencils-holder").html("");
				for(var i=0;i<=data.data.length;i++){
					
					caseProject.renderItemProject(data.data[i]);
				}
				
			}
			caseProject.toggleLoading(false);
		}).error(function(data, status, headers, config) {
			if(status=="error") showStickyErrorToast(data.responseText);
			console.log(data);
			console.log(status);
			caseProject.toggleLoading(false);
		});
	}
};

var modelsProjects = {
	name : "",
	/**
	 * Remove project 
	 **/
	remove : function(id){
		$.get("/api/projects/remove?id="+id,{})
		.success(function(data, status, headers, config) {
			
			if(data.error){
				showStickyErrorToast(data.message);
				
			}else{
				reloadPage();
			}
		}).error(function(data, status, headers, config) {
			if(status=="error") showStickyErrorToast(data.responseText);
			console.log(data);
			console.log(status);
		});
	},
	newProject : function(name){


		
	}
};

$(".class_remove_project").live('click',function(e){
	var uuid = $(this).attr('uuid');
	jConfirm('Are you sure you want to permanently remove this project?', 'Delete project', function(r) {
		if(r){
			
			
			$(".div-item"+uuid).remove();
			caseProject.closeBoardUpload();
			caseProject.closeMenu();
			modelsProjects.remove(uuid);
			
		}else{
		
		}
	});
	
});
$("#search-input").live('keyup',function(e){
	if(e.keyCode ==13){
		caseProject.search($(this).val());
	}
});
$(".li-project-name").live('click',function(e){
	
	caseProject.setColorItem($(this));
	currentProject.id = $(this).attr('uuid');
	
	currentProject.name = $(this).attr('name');
	try{
	    $('#fileupload').fileupload({
			// Uncomment the following to send cross-domain cookies:
			//xhrFields: {withCredentials: true},
			url: "/api/upload/file?pid="+currentProject.id
		});
	}catch(e){
		
	}
	caseProject.openMenu();
	caseProject.closeBoardUpload();
	
});
